/**
  ************************************* Copyright ****************************** 
  *
  *        (C) Copyright 2023,Aerospace Newsky Technology Co., Ltd
  *                            All Rights Reserved
  *                              
  *                           https://www.js1959.com
  *    
  * FileName   : miscutil_qc.c   
  * Version    : v1.0		
  * Author     : gongx			
  * Date       : 2024-09-09         
  * Description:    
  * Function List:  
  	1. ....
  	   <version>: 		
  <modify staff>:
  		  <data>:
   <description>:  
  	2. ...
  ******************************************************************************
 */

#include "miscutil_qc.h"
#include "miscutil.h"


QC_DATA qcm_CalculateVaporPressure(const QC_DATA *temperature, const QC_DATA *humidity)
{
	float	vp;
	QC_DATA	ret;

	vp = CalculateVaporPressure(temperature->value.int16Value / (float)10, humidity->value.uint16Value);
	ret.value.uint16Value = round_to_int(vp * 10);
	if (qc_IsGood(temperature->qcFlag) && qc_IsGood(humidity->qcFlag))
	{
		ret.qcFlag = QC_GOOD;
	}
	else if (!qc_IsEnabled(temperature->qcFlag) || !qc_IsEnabled(humidity->qcFlag))
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
		ret.qcFlag = qc_SetDisabled(ret.qcFlag);
	}
	else if (!qc_IsExist(temperature->qcFlag) || !qc_IsExist(humidity->qcFlag))
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
		ret.qcFlag = qc_SetNotExist(ret.qcFlag);
	}
	else if (qc_IsErroneous(temperature->qcFlag) || qc_IsErroneous(humidity->qcFlag))
	{
		ret.qcFlag = QC_ERRONEOUS;
	}
	else if (qc_IsInconsistent(temperature->qcFlag) || qc_IsInconsistent(humidity->qcFlag))
	{
		ret.qcFlag = QC_INCONSISTENT;
	}
	else if (qc_IsDoubtful(temperature->qcFlag) || qc_IsDoubtful(humidity->qcFlag))
	{
		ret.qcFlag = QC_DOUBTFUL;
	}
	else if (!qc_IsChecked(temperature->qcFlag) || !qc_IsChecked(humidity->qcFlag))
	{
		ret.qcFlag = QC_NOT_CHECKED;
	}
	else
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
	}
	return ret;
}
QC_DATA qcm_CalculateDewPoint(const QC_DATA *temperature, const QC_DATA *humidity)
{
	float	dp;
	QC_DATA	ret;

	dp = CalculateDewPoint(temperature->value.int16Value / (float)10, humidity->value.uint16Value);
	ret.value.uint16Value = round_to_int(dp * 10);
	if (qc_IsGood(temperature->qcFlag) && qc_IsGood(humidity->qcFlag))
	{
		ret.qcFlag = QC_GOOD;
	}
	else if (!qc_IsEnabled(temperature->qcFlag) || !qc_IsEnabled(humidity->qcFlag))
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
		ret.qcFlag = qc_SetDisabled(ret.qcFlag);
	}
	else if (!qc_IsExist(temperature->qcFlag) || !qc_IsExist(humidity->qcFlag))
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
		ret.qcFlag = qc_SetNotExist(ret.qcFlag);
	}
	else if (qc_IsErroneous(temperature->qcFlag) || qc_IsErroneous(humidity->qcFlag))
	{
		ret.qcFlag = QC_ERRONEOUS;
	}
	else if (qc_IsInconsistent(temperature->qcFlag) || qc_IsInconsistent(humidity->qcFlag))
	{
		ret.qcFlag = QC_INCONSISTENT;
	}
	else if (qc_IsDoubtful(temperature->qcFlag) || qc_IsDoubtful(humidity->qcFlag))
	{
		ret.qcFlag = QC_DOUBTFUL;
	}
	else if (!qc_IsChecked(temperature->qcFlag) || !qc_IsChecked(humidity->qcFlag))
	{
		ret.qcFlag = QC_NOT_CHECKED;
	}
	else
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
	}
	return ret;
}

QC_DATA qcm_CalculateSeaSurfacePressure(const QC_DATA *stationBaro, float altitudeElevation,
	const QC_DATA *airTemperature, const QC_DATA * airTemperature12HoursAgo)
{
	QC_DATA	ret;

	ret.qcFlag = QC_GOOD;

	if (((stationBaro->qcFlag & 0xF0) == QC_NOT_EXIST) ||
		((airTemperature->qcFlag & 0xF0) == QC_NOT_EXIST) ||
		((airTemperature12HoursAgo->qcFlag & 0xF0) == QC_NOT_EXIST))
	{
		ret.qcFlag = QC_NOT_EXIST | QC_NOT_APPLICABLE;
		return	ret;
	}

	if ((stationBaro->qcFlag != QC_GOOD) || (airTemperature->qcFlag != QC_GOOD) ||
		(airTemperature12HoursAgo->qcFlag != QC_GOOD))
	{
		ret.qcFlag = QC_NOT_APPLICABLE;
	}
	ret.value.uint16Value = round_to_int(CalculateSeaSurfacePressure(
		stationBaro->value.uint16Value / (float)10,
		altitudeElevation,
		airTemperature->value.int16Value / (float)10,
		airTemperature12HoursAgo->value.int16Value / (float)10) * 10);

	return	ret;
}

